extends Chara
var utils = globalData.infoDs["g_alxUtils"]
const bf = globalData.infoDs["g_alxBuffs"]
const us = globalData.infoDs["g_alxUtils"]
const base = globalData.infoDs["g_alxBase"]
func _info():
	pass
func _connect():
	._connect()
	pass

class alxatk:
	extends AtkInfo
	var type="wu"
	func copy(atkInfo:AtkInfo):
		rate = 1
		hitCha = atkInfo.hitCha
		atkCha = atkInfo.atkCha
		hitObj = atkInfo.hitObj
		atkObj = atkInfo.atkObj
		hurtType = atkInfo.hurtType
		hurtVal = atkInfo.hurtVal
		atkType = atkInfo.atkType
		atkVal = atkInfo.atkVal
		isCri = atkInfo.isCri
		isAtk = atkInfo.isAtk
		isMiss = atkInfo.isMiss

signal onChangeHp
var charaInfo 
signal castCdSkill
var baseId=""
var camp = "其他"
var campTmp = null
var killNum = 0
var file = File.new()
var pic = null
var xb = "无"

func _extInit():
	._extInit()
	chaName = "基础角色"
	attCoe.atkRan = 1
	attCoe.maxHp = 100
	attCoe.atk = 0
	attCoe.mgiAtk = 0
	attCoe.def = 0
	attCoe.mgiDef = 0
	lv = 1
	evos = []
	if not is_connected("onPressed", self, "chaInfo"):
		connect("onPressed", self, "chaInfo")

func _onBattleEnd():
	._onBattleEnd()
	plusHp(att.maxHp*1)
	if shield > 0:
		shield = 0

#变更护盾值
var shield:int = 0
var shieldLimit
func changeShield(num):
	if num > 0:
		num *= att.reHp + 1
	shield += num
	if shieldLimit == null and shield > att.maxHp:
		shield = att.maxHp
	elif shieldLimit != null and shield > shieldLimit:
		shield = shieldLimit
	shield = int(shield)

func _onHurt(atkInfo):
	._onHurt(atkInfo)
	var xb2 = atkInfo.atkCha.get("xb")#敌方系别
	var n = 1.0
	if xb2 && atkInfo.atkType == Chara.AtkType.NORMAL:
		for i in dit:
			var pos1 = i.find(xb)
			var pos2 = i.find(xb2)
			if pos1 != -1 && pos2 != -1:
				if pos1 == 0:#克制
					n = 0.75
				else:#被克制
					n = 1.25
				break
	atkInfo.hurtVal *= n
	if atkInfo.atkCha == null:
		atkInfo.hurtVal = 0
	#护盾判定
	if shield == 0:return
	var bonus = 1
	if atkInfo.hurtType == Chara.HurtType.MGI:
		bonus = 1.3
	shield -= atkInfo.hurtVal * bonus
	if shield >= 0:
		if atkInfo.atkCha.has_method("increDmgNum"):
			atkInfo.atkCha.increDmgNum(atkInfo.hurtVal * bonus)
		atkInfo.hurtVal = 0
	else:
		atkInfo.hurtVal -= shield
		if atkInfo.atkCha.has_method("increDmgNum"):
			atkInfo.atkCha.increDmgNum(shield)
		shield = 0


#周围一格
var aroundCells = [Vector2(-1,0),Vector2(1,0),Vector2(0,1),Vector2(0,-1),Vector2(1,1),Vector2(-1,-1),Vector2(1,-1),Vector2(-1,1)]
func getAroundChas(cell, allys = true):
	var chas = []
	for i in aroundCells:
		var cha = matCha(cell+i)
		if cha != null && (allys && cha.team == team || not allys && cha.team != team):
			chas.append(cha)
	return chas
#周围十字范围
var nearCells = [Vector2(-1,0),Vector2(1,0),Vector2(0,1),Vector2(0,-1)]
func getNearChas(cell, allys = true):
	var chas = []
	for i in nearCells:
		var cha = matCha(cell+i)
		if cha != null && (allys && cha.team == team || not allys && cha.team != team):
			chas.append(cha)
	return chas

#临时属性相关
var tmpAtt = {}
var tmpAtt2 = {}

#角色面板优化
func chaInfo(cha):
	yield(cha.reTimer(0.05),"timeout")
	var xb1 = cha.xb#属性
	var cmp = cha.camp
	var charaInfo = sys.get_node("/root/topUi/charaInfoMsg")
	var txt = charaInfo.get_node("txt")
	var split = txt.bbcode_text.rsplit("\n")
	var res = ""
	for i in range(split.size()):
		var d1 = int(cha.att.def / (100 + cha.att.def) * 100) #物防减免
		var d2 = int(cha.att.mgiDef / (100 + cha.att.mgiDef) * 100)	#魔抗减免
		var d3 = int(10000.0/max(1,cha.att.cd*100+100)) #实际用时
		if i == 0:
			res += split[i] + "[color=#FFFFFF]     [color=#FFFF00]系别[/color]:{2} [color=#FFFF00]击杀[/color]:{1}    [color=#FFFF00]攻击距离[/color]:[color=#FFFF00]{0}[/color]\n".format({"0":cha.att.atkRan,"1":cha.killNum,"2":xb1})
		elif i == 1:
			res += "[color=#66FF66]体力[/color]｜[color=#00DD00]受治疗效果[/color]:[color=#66FF66]{0}/{1}[/color]｜[color=#00DD00]{2}%[/color]           [color=#FF7744]增伤[/color]｜[color=#B8860B]承伤[/color]:[color=#FF7744]{3}%[/color]｜[color=#B8860B]{4}%[/color]\n".format({"0":int(cha.att.hp),"1":int(cha.att.maxHp),"2":cha.att.reHp*100,"3":(cha.att.atkR+1)*100,"4":(1-cha.att.defR)*100})
		elif i == 2:
			res += "[color=#FFAA33]物攻(吸血)[/color]｜[color=#66FFFF]魔强(吸血)[/color]:[color=#FFAA33]{0}({1}%)[/color]｜[color=#66FFFF]{2}({3}%)[/color]           [color=#CC0000]暴击[/color]｜[color=#FFCC22]爆伤[/color]:[color=#CC0000]{4}%[/color]｜[color=#FFCC22]{5}%[/color]\n".format({"0":int(cha.att.atk),"1":cha.att.suck*100,"2":int(cha.att.mgiAtk),"3":cha.att.mgiSuck*100,"4":cha.att.cri*100,"5":(cha.att.criR+2)*100})
		elif i == 3:
			res += "[color=#FFA488]护甲(减免)[/color]｜[color=#FFB7DD]魔抗(减免)[/color]:[color=#FFA488]{0}({1}%)[/color]｜[color=#FFB7DD]{2}({3}%)[/color]            [color=#00BBFF]攻速[/color]｜[color=#20B2AA]闪避[/color]:[color=#00BBFF]{4}%[/color]｜[color=#20B2AA]{5}%[/color]\n".format({"0":int(cha.att.def),"1":d1,"2":int(cha.att.mgiDef),"3":d2,"4":(cha.att.spd-1)*100,"5":cha.att.dod*100})
		elif i == 4:
			res += "[color=#5500DD]护甲穿透[/color]｜[color=#5500DD]魔抗穿透[/color]:[color=#5500DD]{2}({0}%)[/color]｜[color=#5500DD]{3}({1}%)[/color]\n".format({"0":cha.att.penL*100,"1":cha.att.mgiPenL*100,"2":cha.att.pen,"3":cha.att.mgiPen})
			res += "[color=#0066FF]充能速度[/color]｜[color=#0066FF]实际冷却[/color]:[color=#0066FF]{0}%[/color]｜[color=#0066FF]{1}%[/color]\n".format({"0":cha.att.cd*100,"1":d3}) 
		else:
			res += split[i] + "\n"
	txt.bbcode_text = res
	if not is_instance_valid(pic):
		var paths = ""
		if cha.id.find("calx") != -1:
			paths = cha.id.replace("calx","")
		else:
			paths = cha.id.replace("cex___","")
		#print(paths+".png")
		pic = utils.cTr("alxtupian/" + paths +".png")
		if pic != null:
			#print(pic.get_texture().size())
			pic.rect_position = Vector2(1100, 50)
		charaInfo.add_child(pic)	

#选取环形内单位
func getAnCellChas(cell:Vector2,ran,mTeam):
	var lists = []
	var cellran = []
	var n = ran + 2
	for x in range(n):
		for y in range(n):
			var a:Vector2
			a.x = x-1
			a.y = y-1
			cellran.append(a)
	for i in cellran:
		var v = i + cell
		if !sys.main.isMatin(v) || matCha(v) == null:continue
		match mTeam:
			0:
				lists.append(matCha(v))
			1:
				if matCha(cell).team != matCha(v).team:
					lists.append(matCha(v))
			2:
				if matCha(cell).team == matCha(v).team:
					lists.append(matCha(v))
	return lists

#杀敌数统计
func _onKillChara(atkInfo:AtkInfo):
	._onKillChara(atkInfo)
	killNum += 1
var dit = [
"水火","水暗","水龙","火木","火冰","火圣灵","火魔","木水","木光","光妖精","光机械","暗光","暗神秘","圣灵木","圣灵上古","王妖精","王圣灵","神秘火","神秘机械","神秘龙"]#克制写前被克制写后
func typeHurtCha(cha,value,hurttype:int,atktype:int,xb=self.xb,criif=false):#有属性，xb：普通攻击可不填，技能攻击符合自身可不填
	var xb2 = cha.get("xb")#敌方系别
	var n = 1.0
	if xb2:
		var zuhe = xb + xb2
		#print(xb)
		#print(xb2)
		for i in dit:
			var pos1 = i.find(xb)
			var pos2 = i.find(xb2)
			if pos1 != -1 && pos2 != -1:
				if pos1 == 0:#克制
					n = 1.25
				else:#被克制
					n = 0.75
				break
	alxHurtCha(cha,n*value,hurttype,atktype,xb,criif)

func alxHurtCha(cha,value,hurttype:int,atktype:int,xb=self.xb,criif=false):#无属性
	if cha == null:return
	if atktype == AtkType.NORMAL:
		criif = true
	var atkinfo = alxatk.new()
	atkinfo.type = xb
	atkinfo.rate = 1
	atkinfo.hitCha = cha
	atkinfo.atkCha = self
	atkinfo.hitObj = cha
	atkinfo.atkObj = self
	atkinfo.hurtType = hurttype
	atkinfo.hurtVal = value
	atkinfo.atkType = atktype
	atkinfo.atkVal = value
	if criif && sys.rndPer(att.cri * 100):
		atkinfo.isCri = true
		atkinfo.hurtVal *= 2 + att.criR
		atkinfo.atkVal *= 2 + att.criR
	else: 
		atkinfo.isCri = false

	if atktype == Chara.AtkType.NORMAL:
		atkinfo.isAtk = true
	else:
		atkinfo.isAtk = false
	atkinfo.isMiss = false
	cha.hurtArit(atkinfo)

signal summChara
func summChara(id, extendItem = false, summTeam = team):
	var cha = newChara(id, self.cell)
	if cha == null:
		#召唤失败，尝试在其他位置重新召唤
		var cells = getAllCells(team) 
		for i in cells:
			if matCha(i) == null:
				cha = newChara(id, i)
				break
	if cha != null:
		cha.isSumm = true
		emit_signal("summChara", cha)
		if extendItem:
			for i in items:
				var item = sys.newItem(i.id)
				if i.get("upgraded") == true && i.has_method("upgrade"):
					item.upgrade()
				cha.addItem(item)
	return cha

func getAllCells(team):
	if team == 1:
		return cellsLeft + cellsRight
	elif team == 2:
		return cellsRight + cellsLeft
func getAllyCells(team):
	if team == 1:
		return cellsLeft
	elif team == 2:
		return cellsRight

#左半场
const cellsLeft = [
	Vector2(0,0), Vector2(1,0), Vector2(2,0), Vector2(3,0), Vector2(4,0),

	Vector2(0,1), Vector2(1,1), Vector2(2,1), Vector2(3,1), Vector2(4,1),

	Vector2(0,2), Vector2(1,2), Vector2(2,2), Vector2(3,2), Vector2(4,2),

	Vector2(0,3), Vector2(1,3), Vector2(2,3), Vector2(3,3), Vector2(4,3),

	Vector2(0,4), Vector2(1,4), Vector2(2,4), Vector2(3,4), Vector2(4,4),

	Vector2(0,5), Vector2(1,5), Vector2(2,5), Vector2(3,5), Vector2(4,5)
]
#右半场单元格
const cellsRight = [
	Vector2(5,0), Vector2(6,0), Vector2(7,0), Vector2(8,0), Vector2(9,0),

	Vector2(5,1), Vector2(6,1), Vector2(7,1), Vector2(8,1), Vector2(9,1),

	Vector2(5,2), Vector2(6,2), Vector2(7,2), Vector2(8,2), Vector2(9,2),

	Vector2(5,3), Vector2(6,3), Vector2(7,3), Vector2(8,3), Vector2(9,3),

	Vector2(5,4), Vector2(6,4), Vector2(7,4), Vector2(8,4), Vector2(9,4),

	Vector2(5,5), Vector2(6,5), Vector2(7,5), Vector2(8,5), Vector2(9,5)
]

signal normalAtkChara
func normalAtkChara(cha):
	emit_signal("normalAtkChara", cha)
	.normalAtkChara(cha)

func normalAtk(cha):
	.normalAtkChara(cha)